package cn.openea.perpartner.msg.controller;

import cn.openea.perpartner.msg.entity.User;
import cn.openea.perpartner.msg.iservice.IUserService;
import cn.openea.perpartner.msg.mapper.UserMapper;
import cn.openea.perpartner.msg.utils.Tools;
import cn.openea.perpartner.msg.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

/**
 * @Name: 测试API
 * @author: Mr.Yun
 * @Created: 2022/11/7 19:49
 */
@Api(tags = "测试API")
@RestController
@RequestMapping("/api")
public class TestApi {

    @Autowired
    WxMpService wxMpService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    IUserService userService;

    @Value("${wxmp.appid}")
    String appid;

    @ApiOperation("测试获取微信公众号token")
    @RequestMapping(value = "/getmptoken", method = RequestMethod.POST)
    public R getMpToken() throws WxErrorException {
        return R.ok().put("token", wxMpService.getAccessToken());
    }

    @ApiOperation("插入若干条记录")
    @RequestMapping(value = "/savedata", method = RequestMethod.POST)
    public R savedata(@RequestBody @ApiParam(name = "写入的记录数", value = "请输入插入条数，必填") int num) {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            User user = new User();
            user.setName(Tools.getRandom(2, 6).toLowerCase());
            user.setPhon("178" + Tools.getRandom(1, 8));
            userList.add(user);
        }
        boolean b = userService.saveBatch(userList);
        if(b){
            return R.ok().put("num", num);
        }
        return R.error("批量插入错误");
}

    @ApiOperation("获取表-使用分页")
    @RequestMapping(value = "/getpage", method = RequestMethod.POST)
    public R getRecordByPage() {
        // 分页查询
        Page<User> page = new Page<>(1, 20);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.gt("id", 2);
        IPage<User> iPage = userMapper.selectPage(page, queryWrapper);
        List<User> records = iPage.getRecords();

        /**
         * 返回结果
         * {
         *   "code": 200,
         *   "datalist": [{user1},{user2}]
         * }
         */
        return R.ok().put("datalist", records);
    }

    /**
     * 每天01:03分执行一次 ，这个值在 application.yml 进行配置
     * base.cron=0 3 1 * * ?
     *  定时脚本
     */
    @Scheduled(cron = "${base.cron}")
    @ApiOperation("执行定时任务")
    @RequestMapping(value = "/doscheduled", method = RequestMethod.POST)
    public R scheduledMethod(){
        // TODO：在这里做定时任务
        System.out.println("定时执行任务......");
        Map map = new HashMap<>();
        map.put("appid", appid);
        map.put("doscheduled", "doscheduled");
        map.put("datetime", new Date());
        map.put("rows", 100);
        return R.ok().putAll(map);
    }

}
